package cn.com.youyuan.ui;

import cn.com.youyuan.common.J2meStrRes;
import cn.com.youyuan.common.J2meUtility;
import cn.com.youyuan.display.GraphicsProxy;
import cn.com.youyuan.display.MainCanvas;
import cn.com.youyuan.models.Action;
import cn.com.youyuan.models.KeyEvent;
import cn.com.youyuan.models.UIContainer;
import cn.com.youyuan.models.UIElement;

public class Button extends UIElement
{
	private static final int BUTTON_WIDTH_OFFSET = 28;
	private static final int BUTTON_HEIGHT_OFFSET = 2;
	private static final int BUTTON_MIN_WIDTH = 30;
	private static final int COLOR_WHITE = 0xFFFFFF;
	private Anchor m_anchor;

	public Anchor getAnchor() {
		return m_anchor;
	}

	public void setAnchor(Anchor m_anchor) {
		this.m_anchor = m_anchor;
	}

	private String m_buttonValue;

	public Button(UIContainer parent, int left, int top, int width, int height, String buttonValue)
	{
		super(parent, left, top, 0, 0);

		m_buttonValue = buttonValue == null ? J2meStrRes.STR_EMPTY : buttonValue;
		m_height = s_defaultFontHeight + BUTTON_HEIGHT_OFFSET;
		m_width = Math.max(s_defaultFont.stringWidth(m_buttonValue) + BUTTON_WIDTH_OFFSET, BUTTON_MIN_WIDTH);
	}

	public boolean canReceiveFocus()
	{
		return true;
	}

	public void setWidth(int width)
	{

	}

	public void setHeight(int height)
	{

	}

	public int getWidth()
	{
		return Math.max(s_defaultFont.stringWidth(m_buttonValue) + BUTTON_WIDTH_OFFSET, BUTTON_MIN_WIDTH);
	}

	public int getHeight()
	{
		return s_defaultFontHeight + BUTTON_HEIGHT_OFFSET;
	}

	protected int getMinHeight()
	{
		return getHeight();
	}

	public int onKeyClick(int keyCode)
	{
		if (keyCode == KeyEvent.KEY_OK)
		{
			popUIEvent(Action.EVENT_BUTTON_ON_CLICK, this);
			return EVENT_RESULT_HANDLED;
		}

		return EVENT_RESULT_NULL;
	}

	public int onPointerDrag(int x, int y)
	{
		return 0;
	}

	public int onPointerPress(int x, int y)
	{
		if (J2meUtility.containsPoint(x, y, m_left, m_top, m_width, m_height))
		{
			return EVENT_RESULT_REPAINT | EVENT_RESULT_HANDLED;
		}

		return EVENT_RESULT_NULL;
	}

	public int onPointerRelease(int x, int y)
	{
		if (J2meUtility.containsPoint(x, y, m_left, m_top, m_width, m_height))
		{
			return onKeyClick(KeyEvent.KEY_OK);
		}

		return EVENT_RESULT_NULL;
	}

	public boolean paint(GraphicsProxy g, int offsetHeight, boolean isFocused)
	{
		final int buttonWidth = s_defaultFont.stringWidth(m_buttonValue) + BUTTON_WIDTH_OFFSET;
		final int buttonHeight = s_defaultFontHeight + BUTTON_HEIGHT_OFFSET;

		if (isFocused)
		{
			g.setColor(MainCanvas.getSkinColorValue(MainCanvas.INDEX_FOCUSED_DEEP));
			J2meUtility.drawGradient(g, m_left, m_top + offsetHeight, buttonWidth, buttonHeight, MainCanvas.getSkinColorValue(MainCanvas.INDEX_BUTTON_LIGHT), MainCanvas
					.getSkinColorValue(MainCanvas.INDEX_BUTTON_DEEP));
			g.setColor(MainCanvas.getSkinColorValue(MainCanvas.INDEX_BUTTON_FOCUSED));
			g.drawRect(m_left, m_top + offsetHeight, buttonWidth, buttonHeight);
		}
		else
		{
			J2meUtility.drawGradient(g, m_left, m_top + offsetHeight, buttonWidth, buttonHeight, MainCanvas.getSkinColorValue(MainCanvas.INDEX_BUTTON_LIGHT), MainCanvas
					.getSkinColorValue(MainCanvas.INDEX_BUTTON_DEEP));

		}

		g.setColor(COLOR_WHITE);
		g.drawString(m_buttonValue, m_left + (BUTTON_WIDTH_OFFSET >> 1), m_top + (BUTTON_HEIGHT_OFFSET >> 1) + offsetHeight, 0);

		return false;
	}
}